package com.tannm.doan.taskmanager.activities;

import com.tannm.doan.taskmanager.CommonActivity;
import com.tannm.doan.taskmanager.database.DataRowNote;
import com.tannm.doan.taskmanager.database.DataTable;
import com.tannm.doan.widgets.*;

import com.tannm.doan.taskmanager.R;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.ImageButton;

//New Note activity
public class ActivityNote extends CommonActivity {
	// private fields
	private DataRowNote dataRow = null;
	private DataTable dataTable = null;

	// views
	private TouchEdit edSubject = null;
	private ImageButton btnDelete = null;
	private ImageButton btnSave = null;

	// methods
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.note);

		// initialize objects
		dataRow = new DataRowNote(userdb);
		dataTable = new DataTable(dataRow);

		// check startup mode and open data
		if (getStartMode() == StartMode.EDIT)
			if (!openDataForEdit(dataTable))
				finish();

		// initialize views
		initViews();
		initState();
	}

	@Override
	public void onStart() {
		super.onStart();

	}

	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	public void onPause() {
		super.onPause();

	}

	private void initViews() {
		edSubject = (TouchEdit) findViewById(R.id.edNoteSubject);
		edSubject.setOnOpenKeyboard(new TouchEdit.OnOpenKeyboard() {
			public void OnOpenKeyboardEvent() {
				KeyboardWidget.open(ActivityNote.this, edSubject.getText()
						.toString());
			}
		});

		btnSave = (ImageButton) findViewById(R.id.btnNoteSave);
		btnSave.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				saveData();
			}
		});

		btnDelete = (ImageButton) findViewById(R.id.btnNoteDelete);
		btnDelete.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				deleteData();
			}
		});
	}

	private void initState() {
		// title
		String sSubTitle = utils.getResStr(R.string.titleDefaultNote);

		// INSERT MODE
		if (getStartMode() == StartMode.NEW) {
			sSubTitle = utils.getResStr(R.string.titleNewNote);
			btnDelete.setVisibility(View.INVISIBLE);
		}

		// EDIT MODE
		if (getStartMode() == StartMode.EDIT) {
			sSubTitle = utils.getResStr(R.string.titleEditNote);
			edSubject.setText(dataRow.getSubject());
			btnDelete.setVisibility(View.VISIBLE);
		}

		restoreStateFromFreezeIfRequired();

		setActivityTitle(sSubTitle);

		// set focus to subject
		edSubject.requestFocus();
		if (getStartMode() == StartMode.EDIT)
			edSubject.setSelection(edSubject.length());
	}

	public void saveData() {
		dataRow.setSubject(edSubject.getText().toString());

		if (saveDataToTable(dataTable))
			closeActivity(dataTable);
	}

	public void deleteData() {
		if (deleteDataFromTable(dataTable))
			closeActivity(dataTable);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		Bundle extras = CommonActivity.getIntentExtras(data);
		if (extras != null) {
			// get KeyboardWidget result
			if ((requestCode == KeyboardWidget.EDIT_TEXT_REQUEST)
					&& (resultCode == RESULT_OK)) {
				String sText = KeyboardWidget.getTextOnActivityResult(
						requestCode, resultCode, extras);
				edSubject.setText(sText);
			}
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		// save controls state
		outState.putString("subject", edSubject.getText().toString());
	}

	@Override
	protected void restoreStateFromFreeze() {
		edSubject.setText(freeze.getString("subject"));
	}

}
